//
//  NSString+TextSize.m
//  EverVCiOSClient
//
//  Created by 张可可 on 15/10/12.
//  Copyright (c) 2015年 EverVC. All rights reserved.
//

#import "NSString+TextSize.h"
#import <CoreText/CTFramesetter.h>
#import "NSString+QT.h"

@implementation NSString (TextSize)

- (CGFloat)textHeightWithWidth:(CGFloat)width fontSize:(CGFloat)fontSize {
    return [self textHeightWithWidth:width font:[UIFont systemFontOfSize:fontSize]];
}
- (CGFloat)textHeightWithWidth:(CGFloat)width font:(UIFont *)font {
    return [self textHeightWithWidth:width textAttributed:@{NSFontAttributeName: font} isHaveLineSpace:NO];
}
- (CGFloat)textHeightWithWidth:(CGFloat)width textAttributed:(NSDictionary *)attributtedDic {
    
//    if ([self isHaveStr] == NO) {
//        return 0;
//    }
//    
//    CGSize size = CGSizeMake(width, MAXFLOAT);
//    
//    CGRect textRect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading attributes:attributtedDic context:nil];
//    
//    return ceilf(textRect.size.height);
    return [self textHeightWithWidth:width textAttributed:attributtedDic isHaveLineSpace:YES];
}
- (CGFloat)textHeightWithWidth:(CGFloat)width textAttributed:(NSDictionary *)attributtedDic isHaveLineSpace:(BOOL)isHave{
    
    if ([self isHaveStr] == NO) {
        return 0;
    }
    
    
    CGSize size = CGSizeMake(width, MAXFLOAT);
    
    CGRect textRect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading attributes:attributtedDic context:nil];
    
    if (isHave == YES && [self containChinese:self] == YES) {
        NSParagraphStyle *sty = attributtedDic[NSParagraphStyleAttributeName];
        UIFont *font = attributtedDic[NSFontAttributeName];
        if (textRect.size.height - font.lineHeight <= sty.lineSpacing) {
            textRect.size.height -= sty.lineSpacing;
        }
    }
    return ceilf(textRect.size.height);
}



- (CGFloat)textWidthWithHeight:(CGFloat)height fontSize:(CGFloat)fontSize {
    return [self textWidthWithHeight:height font:[UIFont systemFontOfSize:fontSize]];
}
- (CGFloat)textWidthWithHeight:(CGFloat)height font:(UIFont *)font {
    return [self textWidthWithHeight:height textAttributed:@{NSFontAttributeName: font}];
}
- (CGFloat)textWidthWithHeight:(CGFloat)height textAttributed:(NSDictionary *)attributtedDic {
    
    if ([self isHaveStr] == NO) {
        return 0;
    }
    
    CGSize size = CGSizeMake(MAXFLOAT, height);
    
    CGRect textRect = [self boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attributtedDic context:nil];
    
    return ceilf(textRect.size.width);
}


+ (NSDictionary *)calculateAttributeDicWithFontSize:(CGFloat)fontSize lineSpacing:(CGFloat)lineSpacing; {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    
    NSMutableDictionary *attributeDic = [NSMutableDictionary dictionary];
    [attributeDic setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attributeDic setObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
    
    return attributeDic;
}
+ (NSDictionary *)attributeDicWithFontSize:(CGFloat)fontSize textColor:(UIColor *)textColor {
    return [self attributeDicWithFontSize:fontSize textColor:textColor lineSpacing:5];
}

+ (NSDictionary *)attributeDicWithFontSize:(CGFloat)fontSize textColor:(UIColor *)textColor lineSpacing:(CGFloat)lineSpacing {
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    
    NSMutableDictionary *attributeDic = [NSMutableDictionary dictionary];
    [attributeDic setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attributeDic setObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];

    if (textColor != nil) {
        [attributeDic setObject:textColor forKey:NSForegroundColorAttributeName];
    }
    
    return attributeDic;
//    NSDictionary *attributeDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
//                                    NSForegroundColorAttributeName: textColor,
//                                    NSParagraphStyleAttributeName: paragraphStyle};
    
//    return attributeDic2;
}
@end

@implementation NSAttributedString (TextSize)

- (CGFloat)textHeightWithWidth:(CGFloat)width {
    if ([self isHaveStr] == NO) {
        return 0;
    }
    
    CGSize size = CGSizeMake(width, MAXFLOAT);
    CGRect textRect = [self boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)   context:nil];
    return ceilf(textRect.size.height);
//    NSRange ra = NSMakeRange(0, 2);
//    NSDictionary *attDic = [self attributesAtIndex:0 effectiveRange:&ra];
//    
//    NSString *str = self.string;
//    
//    CGFloat txtH = [str textHeightWithWidth:width textAttributed:attDic];
    
    
//    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
//    CGSize targetSize = CGSizeMake(width, CGFLOAT_MAX);
//    CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [self length]), NULL, targetSize, NULL);
//    CFRelease(framesetter);
    
//    return txtH;
}

@end
